<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="default" @click="handleImport()">
          <Icon icon="i-ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-button type="primary" @click="handleForm({})" v-auth="'bas:cus:coreKhlxr:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id, khlxrLxrbm: record.khlxrLxrbm })">
          {{ record.khlxrLxrbm }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsBasCusCoreKhlxrList">
  import { unref, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { useQuery } from '/@/hooks/web/usePage';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { coreKhlxrDelete, coreKhlxrListData } from '/@/api/bas/cus/coreKhlxr';
  import { coreKhlxrDisable, coreKhlxrEnable } from '/@/api/bas/cus/coreKhlxr';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';
  import { coreKhdaTreeData } from '/@/api/bas/cus/coreKhda';

  const { t } = useI18n('bas.cus.coreKhlxr');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const getQuery = useQuery();

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('客户联系人管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('客户名称'),
        field: 'basCus.khdaDwqc',
        component: 'Input',
        // componentProps: {
        //   api: coreKhdaTreeData,
        //   canSelectParent: false,
        //   allowClear: true,
        // },
      },
      {
        label: t('联系人名称'),
        field: 'khlxrLxrmc',
        component: 'Input',
      },
      {
        label: t('部门'),
        field: 'khlxrBm',
        component: 'Input',
      },
      {
        label: t('联系电话'),
        field: 'khlxrLxdh',
        component: 'Input',
      },
      {
        label: t('默认联系人'),
        field: 'khlxrMrlxr',
        component: 'Select',
        componentProps: {
          dictType: 'sys_yes_no',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_search_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('联系人编码'),
      dataIndex: 'khlxrLxrbm',
      key: 'a.khlxr_lxrbm',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('联系人名称'),
      dataIndex: 'khlxrLxrmc',
      key: 'a.khlxr_lxrmc',
      sorter: true,
      width: 180,
      align: 'left',
    },
    {
      title: t('部门'),
      dataIndex: 'khlxrBm',
      key: 'a.khlxr_bm',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('联系电话'),
      dataIndex: 'khlxrLxdh',
      key: 'a.khlxr_lxdh',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('默认联系人'),
      dataIndex: 'khlxrMrlxr',
      key: 'a.khlxr_mrlxr',
      dictType: 'sys_yes_no',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('客户名称'),
      dataIndex: 'basCus.khdaDwqc',
      key: 'a.parent_id',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 80,
      align: 'center',
      dictType: 'sys_search_status',
    },
    {
      title: t('更新时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑客户联系人'),
        onClick: handleForm.bind(this, { id: record.id, khlxrLxrbm: record.khlxrLxrbm }),
        auth: 'bas:cus:coreKhlxr:edit',
      },
      {
        icon: 'i-ant-design:stop-outlined',
        color: 'error',
        title: t('停用客户联系人'),
        popConfirm: {
          title: t('是否确认停用客户联系人'),
          confirm: handleDisable.bind(this, record),
        },
        auth: 'bas:cus:coreKhlxr:edit',
        ifShow: () => record.status === '0',
      },
      {
        icon: 'i-ant-design:check-circle-outlined',
        color: 'success',
        title: t('启用客户联系人'),
        popConfirm: {
          title: t('是否确认启用客户联系人'),
          confirm: handleEnable.bind(this, record),
        },
        auth: 'bas:cus:coreKhlxr:edit',
        ifShow: () => record.status === '2',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除客户联系人'),
        popConfirm: {
          title: t('是否确认删除客户联系人'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'bas:cus:coreKhlxr:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: coreKhlxrListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/bas/cus/coreKhlxr/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDisable(record: Recordable) {
    const params = { id: record.id, khlxrLxrbm: record.khlxrLxrbm };
    const res = await coreKhlxrDisable(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleEnable(record: Recordable) {
    const params = { id: record.id, khlxrLxrbm: record.khlxrLxrbm };
    const res = await coreKhlxrEnable(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id, khlxrLxrbm: record.khlxrLxrbm };
    const res = await coreKhlxrDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  async function handleQuery() {
    if (getQuery.value.khdaDwqc) {
      console.log(getQuery.value.khdaDwqc);
      getForm().setFieldsValue({
        'basCus.khdaDwqc': getQuery.value.khdaDwqc,
      });
      reload();
      getForm().setFieldsValue({
        'basCus.khdaDwqc': getQuery.value.khdaDwqc,
      });
    }
  }

  onMounted(async () => {
    handleQuery();
  });
</script>
